package br.com.swp.campestre.session;

import java.util.Arrays;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.validator.Length;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.web.RequestParameter;
import org.jboss.seam.international.StatusMessages;
import org.jboss.seam.log.Log;
import org.jboss.seam.persistence.HibernateSessionProxy;

import br.com.swp.campestre.entity.Lote;
import br.com.swp.campestre.enumeration.StatusLoteFinanceiro;
import br.com.swp.campestre.enumeration.StatusLoteVenda;

@Stateful
@Name("ConsultaLote")
public class ConsultaLoteBean implements ConsultaLote
{
    @Logger private Log log;

    @In StatusMessages statusMessages;
    
	@PersistenceContext
	private EntityManager entityManager;

	@In(create = true, required = false)
	private HibernateSessionProxy hibernateSession;
		
	private Lote lote;
	
	@RequestParameter
	private Long idLote;

	private String value;
    
    private List<Lote> lotes;
    
    private String codigoLote;
    
    private StatusLoteVenda statusLoteVenda;
    
    private StatusLoteFinanceiro statusLoteFinanceiro;

    
	@Out(required=false)
	private List<StatusLoteFinanceiro> statusLoteFinanceiroList;
	@Factory("statusLoteFinanceiroList")
	public void statusLoteFinanceiroList(){
		statusLoteFinanceiroList = Arrays.asList(StatusLoteFinanceiro.values());
	}

	@Out(required=false)
	private List<StatusLoteVenda> statusLoteVendaList;
	@Factory("statusLoteVendaList")
	public void statusLoteVendaList(){
		statusLoteVendaList = Arrays.asList(StatusLoteVenda.values());
	}

    @Begin(join=true)
    public void consultaLote(){
    	
		lote = null;

		Criteria crt = hibernateSession.createCriteria(Lote.class);

		if (codigoLote != null && !codigoLote.equals("")) {
			crt.add(Restrictions.like("codigoLote", codigoLote, MatchMode.ANYWHERE).ignoreCase());
		}
		
		if (statusLoteVenda != null && !statusLoteVenda.equals("")) {
			crt.add(Restrictions.eq("statusLoteVenda", statusLoteVenda));
		}

		if (statusLoteFinanceiro != null && !statusLoteFinanceiro.equals("")) {
			crt.add(Restrictions.eq("statusLoteFinanceiro", statusLoteFinanceiro));
		}
		
//		if ((codigoLote != null && !codigoLote.equals("")) || (statusLoteVenda != null && !statusLoteVenda.equals("")) ) {
//			lotes = crt.list();
//		}
		
		lotes = crt.list();
		
		if(lotes != null && lotes.size() == 1 ){
			lote = lotes.get(0);
		} else {
			lote =  null;
		}    	

		if (lotes == null || lotes.isEmpty()) {
			statusMessages.add("Lote nao encontrada");
		}

    	
    }

    
    @Length(max = 10)
    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }

    @Destroy
    @Remove
    public void destroy() {}

	public void setLote(Lote lote) {
		this.lote = lote;
	}

	public Lote getLote() {
		return lote;
	}

	public void setLotes(List<Lote> lotes) {
		this.lotes = lotes;
	}

	public List<Lote> getLotes() {
		return lotes;
	}

	public void setCodigoLote(String codigoLote) {
		this.codigoLote = codigoLote;
	}

	public String getCodigoLote() {
		return codigoLote;
	}

	public List<StatusLoteVenda> getStatusLoteVendaList() {
		return statusLoteVendaList;
	}

	public void setStatusLoteVendaList(List<StatusLoteVenda> statusLoteVendaList) {
		this.statusLoteVendaList = statusLoteVendaList;
	}

	public StatusLoteVenda getStatusLoteVenda() {
		return statusLoteVenda;
	}


	public void setStatusLoteVenda(StatusLoteVenda statusLoteVenda) {
		this.statusLoteVenda = statusLoteVenda;
	}


	public void setStatusLoteFinanceiro(StatusLoteFinanceiro statusLoteFinanceiro) {
		this.statusLoteFinanceiro = statusLoteFinanceiro;
	}


	public StatusLoteFinanceiro getStatusLoteFinanceiro() {
		return statusLoteFinanceiro;
	}

	public void setStatusLoteFinanceiroList(List<StatusLoteFinanceiro> statusLoteFinanceiroList) {
		this.statusLoteFinanceiroList = statusLoteFinanceiroList;
	}

	public List<StatusLoteFinanceiro> getStatusLoteFinanceiroList() {
		return statusLoteFinanceiroList;
	}










}
